package com.example.goods_manage.controller;

import com.example.goods_manage.dto.request.GoodsRequest;
import com.example.goods_manage.service.GoodsService;
import com.example.goods_manage.utils.AjaxResult;
import com.example.goods_manage.utils.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description: 查询书籍信息
 * <P> Date: 2024/5/29:19:44
 */

@SuppressWarnings({"unchecked", ""})
@RestController
@RequestMapping("goods")
public class GoodsController {
    private static final Logger log = LoggerFactory.getLogger(GoodsController.class);


    @Autowired
    private GoodsService goodsService;

    /**
     * 根据条件查询书籍信息 并且进行分页
    */
    @GetMapping("/findGoods")
    public AjaxResult<PageUtils>  findGoods(GoodsRequest goodsRequest){
        AjaxResult<PageUtils> goods = goodsService.findGoods(goodsRequest);
        PageUtils data = goods.getData();
        int size = data.getList().size();
        return goods;
    }
    /**
     * 根据id 删除商品信息
     */
    @DeleteMapping("/deleteGoods")
    public AjaxResult<Void> deleteGoods(GoodsRequest goodsRequest){
        return goodsService.deleteGoods(goodsRequest);
    }
    /**
     * 根据id 更新商品信息
     */
    @PostMapping("/updateGoods")
    public AjaxResult<Void> updateGoods(@RequestBody GoodsRequest goodsRequest){
        return goodsService.updateGoods(goodsRequest);
    }

    /**
     * 新增书籍信息
    */
    @PostMapping("/addGoods")
    public AjaxResult<Void> addGoods(@RequestBody GoodsRequest goodsRequest){
        return goodsService.addGoods(goodsRequest);
    }

    /**
     * 传入商品名称 进行检索返回接近的名称
    */
    @GetMapping("/retrievalGoodsName")
    public AjaxResult<List<Map>>retrievalGoodsName(String goodsName){
       List<Map> goodsNameList = goodsService.retrievalGoodsName(goodsName);
       return AjaxResult.success(goodsNameList);
    }
}
